#include "graphics.h"
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define ENTER 0x1c0d
void playtoplay(void);
void DrawQp(void);
void SetPlayColor(int t);
void MoveColor(int x,int y);
int around(int x,int y,int t);
void disapear(int x,int y);
int dead(int x,int y,int t);
int other(int x,int y,int t);
void fill(int i,int j,int x);
int search(int i,int j);
void win();
void playWin(int z);
int a[19][19]={0};
int b[19][19]={0};
int c[19][19]={0};
int d[19][19]={0};
int e[19][19]={0};
int key,key1,key2;


void main(void)
     {int gd=DETECT,gr;
      initgraph(&gd,&gr,"c:\\tc");
	  DrawQp();
	  playtoplay();
                    while(1)
                     {key1=bioskey(0);
                      key2=bioskey(0);
                       if(key1==ESC&&key2==ESC)
                        break;
                       else
                       if(key1==ENTER&&key2==ENTER)
						 win();
                       }
	  getch();
	  getch();
      closegraph();
      }
 void DrawQp()
{
 int i,j;
 int a[19][19]={0};
 setbkcolor(BLUE);
 for(i=15;i<=447;i+=24)
 {
  line(15,i,447,i);
  line(i,15,i,447);
	}
 setcolor(0);
 setfillstyle(SOLID_FILL,15) ;
 fillellipse(471,39,10,10);
 }
void playtoplay()
 {
  int x,y,t,i,j;
  t=1;
  while(1)
  {x=471,y=39;
   i=(y-15)/24;
   j=(x-15)/24;
   while(1)
   {SetPlayColor(t);
	fillellipse(x,y,10,10);
	key=bioskey(0);
	if(key==ESC) break;
	else
	if(key==ENTER)
	 {
	  if(x!=471&&a[(y-15)/24][(x-15)/24]!=1&&a[(y-15)/24][(x-15)/24]!=2)
	   {
	     if(t%2==1) a[(y-15)/24][(x-15)/24]=1;
	     else a[(y-15)/24][(x-15)/24]=2;
                        if(around(x,y,t)) other(x,y,t);
                        else
						 if(!other(x,y,t))
						 {
						  a[(y-15)/24][(x-5)/24]=0;
						  continue;
						  }
                        break;
                      }
	  else continue;
	 }
	else
	if(key==LEFT&&x>15)
	  {
	   MoveColor(x,y);
	   SetPlayColor(t);
	   x-=24;
	   fillellipse(x,y,10,10);
	  }
	else
	if(key==RIGHT&&x<447)
	  {
	   MoveColor(x,y);
	   SetPlayColor(t);
	   x+=24;
	   fillellipse(x,y,10,10);
	  }
	else
	if(key==UP&&y>15&&x!=471)
	  {
	   MoveColor(x,y);
	   SetPlayColor(t);
	   y-=24;
	   fillellipse(x,y,10,10);
	   }
	else
	if(key==DOWN&&y<447&&x!=471)
	  {
	   MoveColor(x,y);
	   SetPlayColor(t);
	   y+=24;
	   fillellipse(x,y,10,10);
	   }
	 else
                  if(key==LEFT&&x==15||key==UP&&y==15||key==DOWN&&y==447||
                       key==RIGHT&&x==447||x==471||
                       key!=LEFT&&key!=RIGHT&&key!=UP&&key!=DOWN&&key!=ENTER)
	  continue;
	 else
                   break;
	}
  if(key==ESC) break;
  else t=t%2+1;
   }
  key=bioskey(0);
  if(key!=ESC&&key==ENTER)
  win();
}

 void SetPlayColor(int t)
{
 if(t%2==1)
  setfillstyle(SOLID_FILL,8);
 else
  setfillstyle(SOLID_FILL,15);
 }

 void MoveColor(int x,int y)
{
 if(x>447)
  {
   setfillstyle(SOLID_FILL,BLUE);
   fillellipse(x,y,10,10);
  }
 else
  switch(a[(y-15)/24][(x-15)/24])
   {case 1:
      setfillstyle(SOLID_FILL,8);
      fillellipse(x,y,10,10);
      break;
    case 2:
     setfillstyle(SOLID_FILL,15);
     fillellipse(x,y,10,10);
     break;
    default:
      setfillstyle(LINE_FILL,BLUE);
      fillellipse(x,y,10,10);
      setcolor(15);
     if(x==447) line(x-10,y,x,y);
    else
    if(x==15) line(x,y,x+10,y);
    else line(x-10,y,x+10,y);

    if(y==15) line(x,y,x,y+10);
    else
    if(y==447) line(x,y-10,x,y);
    else	line(x,y-10,x,y+10);
  setcolor(BLUE);
  }
}

int around(int x,int y,int t)
{
  int i,j;
  i=(y-15)/24;
  j=(x-15)/24;
  b[i][j]=1;
  if(a[i-1][j]==0&&(i-1)>=0||a[i+1][j]==0&&(i+1)<=18||a[i][j-1]==0&&(j-1)>=0
	 ||a[i][j+1]==0&&(j+1)<=18)  return 1;
  if(a[i-1][j]==t&&b[i-1][j]!=1&&i!=0) {if(around(x,y-24,t)) return 1;}
  if(a[i+1][j]==t&&b[i+1][j]!=1&&i!=18) {if(around(x,y+24,t)) return 1;}
  if(a[i][j-1]==t&&b[i][j-1]!=1&&j!=0) {if(around(x-24,y,t)) return 1;}
  if(a[i][j+1]==t&&b[i][j+1]!=1&&j!=18) {if(around(x+24,y,t)) return 1;}
  return 0;
}

int dead(int x,int y,int t)
{
  int i,j;
  i=(y-15)/24;
  j=(x-15)/24;
  c[i][j]=1;
  disapear(x,y);
   if(a[i-1][j]==t&&c[i-1][j]!=1)  dead(x,y-24,t);
   if(a[i+1][j]==t&&c[i+1][j]!=1) dead(x,y+24,t);
   if(a[i][j-1]==t&&c[i][j-1]!=1) dead(x-24,y,t);
   if(a[i][j+1]==t&&c[i][j+1]!=1) dead(x+24,y,t);
}

int other(int x,int y,int t)
{ int i,j,p,q,yes;
  yes=0;
  i=(y-15)/24;
  j=(x-15)/24;
  t=t%2+1;
  if(a[i-1][j]==t)
    {
      for(p=0;p<19;p++)
      for(q=0;q<19;q++)
       b[p][q]=0;
      if(!around(x,y-24,t))
       {
        yes=1;dead(x,y-24,t);
        for(p=0;p<19;p++)
         for(q=0;q<19;q++)
         c[p][q]=0;
            }
     }

  if(a[i+1][j]==t)
     {for(p=0;p<19;p++)  for(q=0;q<19;q++) b[p][q]=0;
      if(!around(x,y+24,t))
       {yes=1;dead(x,y+24,t);
        for(p=0;p<19;p++)
         for(q=0;q<19;q++)
         c[p][q]=0;
            }
     }
  if(a[i][j-1]==t)
     {for(p=0;p<19;p++)  for(q=0;q<19;q++) b[p][q]=0;
      if(!around(x-24,y,t))
       {yes=1;dead(x-24,y,t);
        for(p=0;p<19;p++)
         for(q=0;q<19;q++)
         c[p][q]=0;
            }
     }
  if(a[i][j+1]==t)
     {for(p=0;p<19;p++)  for(q=0;q<19;q++) b[p][q]=0;
      if(!around(x+24,y,t))
       {yes=1;dead(x+24,y,t);
        for(p=0;p<19;p++)
         for(q=0;q<19;q++)
         c[p][q]=0;
            }
      }
  return yes;
 }


void disapear(int x,int y)
{int i,j;
 i=(y-15)/24;
 j=(x-15)/24;
 setcolor(BLUE);
   setfillstyle(SOLID_FILL,BLUE);
      fillellipse(x,y,10,10);
      setcolor(15);
     if(x==447) line(x-10,y,x,y);
    else
    if(x==15) line(x,y,x+10,y);
    else line(x-10,y,x+10,y);

    if(y==15) line(x,y,x,y+10);
    else
    if(y==447) line(x,y-10,x,y);
    else	line(x,y-10,x,y+10);
  setcolor(BLUE);
 a[i][j]=0;
}

int search(int i,int j)
{ int yes=0;
  d[i][j]=1;
  if(a[i-1][j]==1&&(i-1)>=0||a[i+1][j]==1&&(i+1)<=18||a[i][j-1]==1&&(j-1)>=0
	 ||a[i][j+1]==1&&(j+1)<=18)  return 1;
  if(a[i-1][j]==2&&(i-1)>=0||a[i+1][j]==2&&(i+1)<=18||a[i][j-1]==2&&(j-1)>=0
	 ||a[i][j+1]==2&&(j+1)<=18)  return 2;

  if(i!=0&&a[i-1][j]==0&&d[i-1][j]!=1) {yes=search(i-1,j);if(yes==1) return 1;else return 2;}
  if(i!=18&&a[i+1][j]==0&&d[i+1][j]!=1){yes=search(i+1,j);if(yes==1) return 1;else return 2;}
  if(j!=0&&a[i][j-1]==0&&d[i][j-1]!=1) {yes=search(i,j-1);if(yes==1) return 1;else return 2;}
  if(j!=18&&a[i][j+1]==0&&d[i][j+1]!=1) {yes=search(i,j+1);if(yes==1) return 1;else return 2;}
}

void fill(int i,int j,int x)
{

  a[i][j]=x;
   if(i!=0&&a[i-1][j]==0)  fill(i-1,j,x);
   if(i!=18&&a[i+1][j]==0) fill(i+1,j,x);
   if(j!=0&&a[i][j-1]==0) fill(i,j-1,x);
   if(j!=18&&a[i][j+1]==0) fill(i,j+1,x);
}


void win()
{
 int i,j,m,n,x;
 for(i=0;i<=18;i++)
  for(j=0;j<=18;j++)
  if(a[i][j]==0)
  {
   x=search(i,j);
   fill(i,j,x);
   }
 m=n=0;
 for(i=0;i<=18;i++)
  for(j=0;j<=18;j++)
  if(a[i][j]==1) m++;
  else n++;
 if(m>=184) playWin(1);
 else
 if(n>=179) playWin(2);
 else  playWin(0);
}

void playWin(int z)
  {
   settextstyle(0,0,2);
   if(z==1)
	 {setcolor(8);
	  outtextxy(471,20,"black win!");
	  }
   else
   if(z==2)
	 {setcolor(15);
	  outtextxy(471,20,"white win!");
	  }
   else
	 {setcolor(RED);
	  outtextxy(471,20,"both you are win!");
	  }
   }























